/**
 * connection statistics service    连接统计服务
 * record connection, login count and list
 */
var Service = function(app) {
  this.serverId = app.getServerId();//当前服务器ID
  this.connCount = 0;//连接计数器
  this.loginedCount = 0;//登录计数器
  this.logined = {};//已登录玩家信息
};

module.exports = Service;

var pro = Service.prototype;


/**
 * Add logined user.            //添加登录的用户
 *
 * @param uid {String} user id
 * @param info {Object} record for logined user
 */
pro.addLoginedUser = function(uid, info) {
  if(!this.logined[uid]) {//如果当前用户未登录过
    this.loginedCount++;//增加登录计数器
  }
  info.uid = uid;
  this.logined[uid] = info;//保存已登录玩家的数据
};

/**
 * Increase connection count
 */
pro.increaseConnectionCount = function() {  //增加连接计数器
  this.connCount++;
};

/**
 * Remote logined user  //移除登录用户
 *
 * @param uid {String} user id
 */
pro.removeLoginedUser = function(uid) {
  if(!!this.logined[uid]) { //如果移除的玩家登录过
    this.loginedCount--;//减少登录计数器
  }
  delete this.logined[uid];//删除对应数据
};

/**
 * Decrease connection count    //减少连接数
 *
 * @param uid {String} uid
 */
pro.decreaseConnectionCount = function(uid) {
  if(this.connCount) {//如果连接次数不为0
    this.connCount--;//减少连接计数器
  }
  if(!!uid) {//如果用户ID传入的话
    this.removeLoginedUser(uid);//移除登录用户
  }
};

/**
 * Get statistics info
 *
 * @return {Object} statistics info
 */
pro.getStatisticsInfo = function() {    //返回统计信息
  var list = [];
  for(var uid in this.logined) {
    list.push(this.logined[uid]);//将所有已登录的玩家的信息保存到数组中
  }
  //返回  服务器ID                   所有的连接数                      登录数                             登录列表（所有已登录的玩家的数据）
  return {serverId: this.serverId, totalConnCount: this.connCount, loginedCount: this.loginedCount, loginedList: list};
};
